package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.BatteryType;
import org.kjtc.entity.Page;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.BatteryTypeService;
import org.kjtc.util.Tools;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BatteryTypeController extends BaseController{

	@Resource(name = "batteryTypeService")
	private BatteryTypeService batteryTypeService;

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@RequestMapping(value = "/searchBatteryType")
	@ResponseBody
	public Map<String, Object> searchBatteryType(BatteryType batteryType, String page, String rows){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息查询开始");
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));
		// 获取所有设备数据
		List<BatteryType> list = null;
		try {
			list = batteryTypeService.getBatteryTypePage(batteryType, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息查询异常" + e.getMessage());
			return map;
		}
		map.put("totalCnt",  pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("batteryTypeList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "电池信息查询");
		batteryType = null;
		return map;
	}

	/**
	 * 增加电池信息
	 */
	@ResponseBody
	@RequestMapping(value="/saveBatteryType")
	public Map<String, Object> saveBatteryType(BatteryType batteryType){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息添加开始");
		batteryType.setBatteryDateDTTM(Tools.str2Date(batteryType.getBatteryDateDate()));
		batteryType.setCreateUser(this.getUesr().getUserID());
		batteryType.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = batteryTypeService.saveBatteryType(batteryType);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("PRIMARY".equals(error[3].trim())) {
				map.put("result", "BatteryTypeNoNull");
			} else if("BatteryName".equals(error[3].trim())){
				map.put("result", "BatteryNameNoNull");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "车辆线路信息添加异常:" + e.getMessage());
		}
        logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息添加结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "电池信息添加");
		batteryType = null;
		return map;
	}
	/**
	 * 修改电池信息
	 * @return
	 */
	@RequestMapping(value = "/updateBatteryType")
	@ResponseBody
	public Object updateBatteryType(BatteryType batteryType){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息修改开始");
		batteryType.setBatteryDateDTTM(Tools.str2DateTime(batteryType.getBatteryDateDate()));
		batteryType.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = batteryTypeService.updateBatteryType(batteryType);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("BatteryName".equals(error[3].trim())) {
				map.put("result", "BatteryNameNoNull");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "车辆线路信息修改异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息修改结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "电池信息修改");
		batteryType = null;
		return map;
	}
	/**
	 * 删除一条或多条电池信息
	 */
	@RequestMapping(value = "/deleteBatteryType")
	@ResponseBody
	public Object deleteBatteryType(String batteryTypeList){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息删除开始");
		BatteryType batteryType = new BatteryType();
		if (batteryTypeList != null && !batteryTypeList.isEmpty()) {
			batteryType.setBatteryTypeList(batteryTypeList.split(","));
		}
		int count = 0;
		try {
			count = batteryTypeService.deleteBatteryType(batteryType);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息删除异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电池信息删除结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "电池信息删除");
		batteryType = null;
		return map;
	}
}
